// pages/business/releasemanagement/releasemanagement.js
var utils = require('../../../utils/util.js');
Page({
  data: {
    status: 1,
    curHdIndex: 1,
  },
  onShow: function() {
    var that = this;
    that.storeProductList(that.data.status)
  },
  //tab切换
  tab: function(e) {
    var that = this;
    var statusid = e.currentTarget.dataset.statusid;
    var dataId = e.currentTarget.id;
    var curHdIndex = dataId;
    this.setData({
      curHdIndex: curHdIndex,
      status: statusid
    })
    that.storeProductList(that.data.status)
    //console.log(e);
  },
  //店铺发布的产品列表
  storeProductList: function(status) {
    var that = this;
    utils.http("/api/v1/Storemanage/store_product", {
      store_id: wx.getStorageSync("userType").storeid,
      status: status
    }, function(res) {
      that.setData({
        list: res.datas,
      })
    })
  },
  //去发布
  onGoReleaseTap: function(e) {
    wx.navigateTo({
      url: '../gorelease/gorelease',
    })
  },
  //删除
  deleteProduct: function(e) {
    var that = this;
    wx.showModal({
      title: '删除',
      content: '您确定要删除吗？',
      success: function(res) {
        if (res.confirm) {
          var index = e.currentTarget.dataset.index;
          var id = that.data.list[index].id;
          var store_id = that.data.list[index].store_id;
          utils.http('/api/v1/Storemanage/delete_product', {
            id: id,
            store_id: store_id
          }, function(res) {
            wx.showToast({
              title: '删除成功',
            });
            that.data.list.splice(index, 1)
            that.setData({
              list: that.data.list
            })
          })
        }
      }
    })
  },
  edit: function(e) {
    var that = this;
    wx.navigateTo({
      url: '/pages/business/gorelease/gorelease?edit=true&id=' + that.data.list[e.currentTarget.dataset.index].id,
    })
  },
  again: function(e) {
    var that = this;
    wx.showModal({
      title: '再次发布',
      content: '您确定要再次发布吗？',
      success: function(res) {
        if (res.confirm) {
          var index = e.currentTarget.dataset.index;
          var id = that.data.list[index].id;
          var store_id = that.data.list[index].store_id;
          utils.http('/api/v1/Storemanage/again_publish_product', {
            id: id,
            store_id: store_id
          }, function(res) {
            wx.showToast({
              title: '发布成功',
            });
            that.data.list.splice(index, 1)
            that.setData({
              list: that.data.list
            })
          })
        }
      }
    });
  }
})